Skip to content

feat: User defined initializers in struct definition (for polymorphism) #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

volsa
Copy link
Member

@volsa volsa commented May 15, 2025

No description provided.

volsa and others added 4 commits May 13, 2025 14:21
Still not working, but a base skeleton is available at least

Co-authored-by: Ghaith Hachem <[email protected]>
A very basic example as seen below will now work
```
VAR_GLOBAL
    globalVar : DINT := 10;
END_VAR

TYPE Parent:
    STRUCT
        foo : REF_TO DINT;
    END_STRUCT
END_TYPE

TYPE Child:
    STRUCT
        instance : Parent := (foo := REF(globalVar));
    END_STRUCT
END_TYPE

FUNCTION main
    VAR
        localChild : Child;
    END_VAR

    printf('Value: %d$N', localChild.instance.foo^);
END_FUNCTION
```
For that to work the `indext_struct_type` function needed to add the context defining which struct it currently operates in for the const evaluator to copy over into the `InitData` struct.

Also the resolver needed some extra logic with regards to the init `self` argument, though not entirely sure if needed (specifically the `resolve_reference_expression` needs to match on a `ReferenceExpr`?)

Finally there still seems to be some issues regarding the const evaluator, since one validation ("Some initial values were not generated")  reported an error.

Still, a first prototype 🎉
@volsa volsa changed the title feat: User defined initializers in struct definition feat: User defined initializers in struct definition (for polymorphism) May 16, 2025
Something like `var : StructA := (instanceB := (instanceC := (value :=
5)))` would previously not work because the resolver had incorrect
metadata when recursively descending into these assignments.
Specifically it always assumed the name of the left-hand side to be
whatever the first element was when visiting the right-hand side as a
whole, i.e. it assumed `StructA` when in reality it needed to be
`StructA` for `(instanceB := (...))`, `StructB` for `instanceC := (...)`
and `StructC` for `value := 5`.
@volsa volsa force-pushed the init-of-qualified-references branch from dc677ca to fd43c66 Compare May 19, 2025 12:47
@volsa volsa force-pushed the init-of-qualified-references branch from 65844ed to 0cbece8 Compare May 22, 2025 12:18
@volsa volsa force-pushed the init-of-qualified-references branch from 0cbece8 to fb16d54 Compare May 22, 2025 13:58
@volsa volsa closed this May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant